﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Contract : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoaData(Int32.Parse(Request.QueryString["PaymentID"]));
        }
    }

    private void LoaData(int id)
    {
        int paymentMethodID = id;
        ManagementService process = new ManagementService();
        DataSet ds = new DataSet();
        ds = process.LoadPageContract(paymentMethodID);

        int ServiceCategoryID = Int32.Parse(ds.Tables[0].Rows[0].ItemArray[0].ToString());
        int SubCategoryID = Int32.Parse(ds.Tables[0].Rows[0].ItemArray[1].ToString());

        ddlService.DataSource = process.GetService();
        ddlService.DataBind();
        LoadDataSubCategory(ServiceCategoryID);
        LoadDataPaymentMethod(SubCategoryID);

        ddlService.SelectedIndex = ddlService.Items.IndexOf(ddlService.Items.FindByValue(ServiceCategoryID.ToString()));
        ddlSubCategory.SelectedIndex = ddlSubCategory.Items.IndexOf(ddlSubCategory.Items.FindByValue(SubCategoryID.ToString()));
        ddlPaymentMethod.SelectedIndex = ddlPaymentMethod.Items.IndexOf(ddlPaymentMethod.Items.FindByValue(paymentMethodID.ToString()));

    }
    protected void btnOrder_Click(object sender, EventArgs e)
    {
        if (txtPhone.Text.Equals(""))
        {
            revPhone.IsValid = false;
           
        }
        else
        {
            if (ddlService.SelectedIndex > -1 && ddlSubCategory.SelectedIndex > -1 && ddlPaymentMethod.SelectedIndex > -1)
            {
                
                ManagementCustomer processCus = new ManagementCustomer();
                string CusID = processCus.MakeCustomerID(ddlService.SelectedItem.Text[0].ToString(),Int32.Parse(ddlService.SelectedValue));

                if (processCus.AddCustomer(CusID, txtName.Text, txtPhone.Text, txtEmail.Text, txtAddress.Text))
                {
                    
                    ManagementContract processCon = new ManagementContract();
                    if (processCon.OrderContract(Int32.Parse(ddlService.SelectedValue), Int32.Parse(ddlSubCategory.SelectedValue), Int32.Parse(ddlPaymentMethod.SelectedValue), CusID)){
                        lbSuccess.Visible = true;
                        Label14.Visible = true;
                    }
                    else
                        Response.Write("False"+CusID);
                }
                else
                {
                    Response.Write("Insert Cus False!");
                }
            }
            else
            {
                Response.Write("False!!!");
            }
        }
    }
    protected void ddlService_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadDataSubCategory(Int32.Parse(ddlService.SelectedValue));
        ddlPaymentMethod.Items.Clear();
    }
    
    protected void ddlSubCategory_SelectedIndexChanged(object sender, EventArgs e)
    {
        LoadDataPaymentMethod(Int32.Parse(ddlSubCategory.SelectedValue));
    }
    
    private void LoadDataSubCategory(int ServiceCategoryID)
    {
        ManagemetSubService process = new ManagemetSubService();
        DataSet ds = process.GetSubCategoryByCategoryID(ServiceCategoryID);
        ddlSubCategory.DataSource = ds;
        ddlSubCategory.DataBind();
    }

    private void LoadDataPaymentMethod(int SubCategoryID)
    {
        ManagementPayment process = new ManagementPayment();
        DataSet ds = process.GetPaymentMethodBySubCategoryID(SubCategoryID);
        ddlPaymentMethod.DataSource = ds;
        ddlPaymentMethod.DataBind();

        lbNote.Text = ds.Tables[0].Rows[0].ItemArray[3].ToString();
    }
    protected void ddlPaymentMethod_SelectedIndexChanged1(object sender, EventArgs e)
    {
        Response.Write("Loadpage");
        //LoaData(Int32.Parse((ddlPaymentMethod.SelectedValue).ToString()));
    }
}